// ssigram.y
%{
//includes and global declarations
#include <stdio.h>
#include <string.h>
#include "tree.h"
#include "token.h"


extern int yyerror(char *s);	//yyerror
extern int linenum;		//line number
extern char* fname;		//filename
extern char* yytext;

char errtext[150];		//temp var for error text

%}

%union {
struct node *n;
}

%token <n> WITHPARAMETER 260
%token <n> CREATEVIEW 261
%token <n> EXTRACT 262
%token <n> SELECT 263
%token <n> FROM 264
%token <n> WHERE 265
%token <n> AS 266
%token <n> USING 267
%token <n> IDENT 268
%token <n> COMMA 269

%token <n> DISTINCT 270
%token <n> PERIOD 271
%token <n> LP 272
%token <n> RP 273
%token <n> APOSTRAPHE 274
%token <n> EQ 275
%token <n> LTEQ 276
%token <n> GTEQ 277
%token <n> LT 278
%token <n> GT 279
%token <n> NOT 280
%token <n> IN 281
%token <n> NJ 282
%token <n> MAX 283
%token <n> MIN 284
%token <n> AVG 285
%token <n> COUNT 286
%token <n> EXISTS 287
%token <n> AND 288
%token <n> ICON 289
%token <n> STRING 290
%token <n> SLASH 291


%start file

%type <n> file
%type <n> sql_expression
%type <n> select_expr
%type <n> selectlist
%type <n> selectitem
%type <n> from_expr
%type <n> fromlist
%type <n> where_expr
%type <n> expr
%type <n> item
%type <n> comparison
%type <n> translation_unit
%type <n> extended_expression
%type <n> create_expr
%type <n> plist
%type <n> extract_expr
%type <n> overall_expression


%%
file:
	translation_unit
	{
		$$ = createnode("file", DupToken(), 1, $1);
		tree = $$;
	}
;

translation_unit:
	overall_expression
	{
		$$ = $1;
	}
	| translation_unit overall_expression
	{
		$$ = createnode("translation_unit", DupToken(), 2, $1, $2);
	}
;

overall_expression:
	sql_expression
	{
		$$ = $1;
	}
	|
	extended_expression
	{
		$$ = $1;
	}
;

sql_expression:
	select_expr from_expr where_expr
	{
		$$ = createnode("sql_expression", DupToken(), 3, $1, $2, $3);
	}
;

select_expr:
	SELECT selectlist
	{
		$$ = createnode("select_expr", DupToken(), 2, $1, $2);
	}
	| SELECT
	{
		$$ = $1;
	}
;

selectlist:
	selectitem
	{
		$$ = $1;
	}
	| selectlist COMMA selectitem
	{
		$$ = createnode("selectlist", DupToken(), 3, $1, $2, $3);
	}
;

selectitem:
	IDENT
	{
		$$ = $1;
	}
	| IDENT PERIOD IDENT
	{
		$$ = createnode("selectitem", DupToken(), 3, $1, $2, $3);
	}
;

from_expr:
	FROM fromlist
	{
		$$ = createnode("from_expr1", DupToken(), 2, $1, $2);
	}
	| FROM overall_expression
	{
		$$ = createnode("from_expr2", DupToken(), 2, $1, $2);
	}
;

fromlist:
	IDENT
	{
		$$ = $1;
	}
	| IDENT NJ fromlist
	{
		$$ = createnode("fromlist2", DupToken(), 2, $1, $2, $3);
	}
	| IDENT AS IDENT NJ fromlist
	{
		$$ = createnode("fromlist3.5", DupToken(), 5, $1, $2, $3, $4, $5);
	}
	| IDENT AS IDENT COMMA fromlist
	{
		$$ = createnode("fromlist3", DupToken(), 5, $1, $2, $3, $4, $5);
	}
	| IDENT AS IDENT
	{
		$$ = createnode("fromlist4", DupToken(), 3, $1, $2, $3);
	}
;

where_expr:
	WHERE expr
	{
		$$ = createnode("where_expr1", DupToken(), 2, $1, $2);
//		$$ = $1;
	}
	| where_expr AND expr
	{
		$$ = createnode("where_expr2", DupToken(), 3, $1, $2, $3);
	}
;

expr:
	selectitem comparison item
	{
		$$ = createnode("expr", DupToken(), 3, $1, $2, $3);
	}
;

item:
	selectitem
	{
		$$ = $1;
	}
	| ICON
	{
		$$ = $1;
	}
	| STRING
	{
		$$ = $1;
	}
;

comparison:
	EQ
	{
		$$ = $1;
	}
	| LTEQ
	{
		$$ = $1;
	}
	| GTEQ
	{
		$$ = $1;
	}
	| LT
	{
		$$ = $1;
	}
	| GT
	{
		$$ = $1;
	}
;

extended_expression:
	create_expr
	{
		$$ = $1;
	}
	| extract_expr
	{
		$$ = $1;
	}
;

create_expr:
	CREATEVIEW IDENT WITHPARAMETER plist AS sql_expression
	{
		$$ = createnode("create_expr", DupToken(), 6, $1, $2, $3, $4, $5, $6);
	}
;

plist:
	IDENT
	{
		$$ = $1;
	}
	| plist COMMA IDENT
	{
		$$ = createnode("plist", DupToken(), 3, $1, $2, $3);
	}
;

extract_expr:
	EXTRACT selectlist from_expr USING IDENT AS IDENT where_expr
	{
		$$ = createnode("extract_expr", DupToken(), 8, $1, $2, $3, $4, $5, $6, $7, $8);
	}
;
%%
